package com.globalwave.common.service;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Table;

import com.globalwave.base.BaseServiceImpl;
import com.globalwave.common.ArrayOrderList;
import com.globalwave.common.ArrayPageList;
import com.globalwave.common.C;
import com.globalwave.common.U;
import com.globalwave.common.db.SimpleJDBCDAO;
import com.globalwave.common.entity.SyncBatch;
import com.globalwave.common.entity.SyncBatchCriterion;

public class SyncBatchServiceImpl 
       extends BaseServiceImpl 
       implements SyncBatchService {

	public SyncBatchServiceImpl(Connection connection) {
		this.jdbcDao = new SimpleJDBCDAO() ;
		this.jdbcDao.setConnection(connection) ;
	}
    public SyncBatch create(SyncBatch syncBatch) throws Exception {
        return (SyncBatch) jdbcDao.insert(syncBatch);
    }

    public void update(SyncBatch syncBatch) throws Exception {
        jdbcDao.update(syncBatch) ;
    }

    public void delete(SyncBatch syncBatch) throws Exception {
        // delete cascade 
        //OrganizationSyncBatch orgSyncBatch = new OrganizationSyncBatch() ;
        //orgSyncBatch.setSyncBatch_id(syncBatch.getSyncBatch_id()) ;
        //jdbcDao.delete(OrganizationSyncBatch.class, orgSyncBatch) ;
        
        jdbcDao.delete(syncBatch) ;
        
    }

    public void deleteAll(Long[] ids) throws Exception {
        SyncBatchCriterion criterion = new SyncBatchCriterion() ;
        criterion.setIds(ids) ;
        jdbcDao.delete(SyncBatch.class, criterion) ;
        
    }

    public ArrayPageList<SyncBatch> query(
            SyncBatchCriterion criterion, 
            int pageIndex, 
            int pageSize, 
            ArrayOrderList orderList) throws Exception {
        
        if (orderList == null) {
            orderList = new ArrayOrderList() ;
            //orderList.addAsc("syncBatch_id") ;
        }
        
        return (ArrayPageList<SyncBatch>)jdbcDao.query(criterion, pageIndex, pageSize, orderList, SyncBatch.class);
    }

    public ArrayPageList<SyncBatch> querySyncableBatch(Long startId, Integer threadId) throws Exception {
        
    	SyncBatchCriterion criterion = new SyncBatchCriterion() ;
    	criterion.setStart(startId) ;
    	criterion.setSync_flag("R") ;
    	
    	ArrayOrderList orderList = new ArrayOrderList() ;
    	orderList.addAsc("sync_batch_id") ;
    	
        return (ArrayPageList<SyncBatch>)jdbcDao.query(criterion, -1, 500, orderList, SyncBatch.class);
    }

    public void resetToSyncable(SyncBatch syncBatch) throws Exception {
        //jdbcDao.update(syncBatch) ;
        Map<String, Object> entity = new HashMap<String, Object>() ;
        entity.put("sync_flag", "R") ;// 未处理
        entity.put("$TABLE", SyncBatch.class.getAnnotation(Table.class).name()) ;
        
        Map<String, Object> key = new HashMap<String, Object>() ;
        
        key.put("times_of_fail<", new Integer(3)) ;
        key.put("sync_flag=", C.COMMON_FAIL) ;
        
        /*
        if (syncBatch != null) {
	        if (syncBatch.get() != null) {
	            key.put("biz_id = ", syncBatch.getBiz_id()) ;
	        }
        }*/

        jdbcDao.update(entity, key);
        
    }
    

    public void updateSuccess(SyncBatch syncBatch) throws Exception {
    	syncBatch.setSync_flag(C.COMMON_SUCCESS) ; 
    	syncBatch.setProcess_date(U.currentTimestamp()) ;
        jdbcDao.update(syncBatch) ;
    }

    public void updateFail(SyncBatch syncBatch, Exception e) throws Exception {
    	Integer times = syncBatch.getTimes_of_fail() ;
    	
    	syncBatch.setTimes_of_fail(times == null ? 0 : (times.intValue() + 1)) ;
    	syncBatch.setSync_flag(C.COMMON_FAIL) ;
    	syncBatch.setProcess_date(U.currentTimestamp()) ;
    	
    	final String msg = e.getMessage() ;
    	syncBatch.setRemark(msg.length()>2500?msg.substring(0, 2500):msg) ;
    	
        jdbcDao.update(syncBatch) ;
    }

    
}
